import { Logger, QueryRunner } from 'typeorm'

// TODO 自定义SqlLogger 可以用来存储数据库查询 记录，方便后续查询和排查问题以及查看慢sql等
class SlqLogger implements Logger {
  // 单例模式
  private static instance: SlqLogger

  // 构造函数
  private constructor() {}

  // 获取单例实例
  public static getInstance(): SlqLogger {
    if (!SlqLogger.instance) {
      SlqLogger.instance = new SlqLogger()
    }
    return SlqLogger.instance
  }

  // 记录正常查询
  logQuery = (query: string, parameters?: any[], queryRunner?: QueryRunner): any => {
    // log query
  }

  // 记录异常查询
  logQueryError = (error: string | Error, query: string, parameters?: any[], queryRunner?: QueryRunner): any => {
    // log query error
  }

  // 记录慢查询
  logQuerySlow = (time: number, query: string, parameters?: any[], queryRunner?: QueryRunner): any => {
    // log query slow
  }

  // 记录schema构建过程
  logSchemaBuild = (message: string, queryRunner?: QueryRunner): any => {
    // log schema build
  }

  // 记录迁移过程
  logMigration = (message: string, queryRunner?: QueryRunner): any => {
    // log migration
  }

  // 记录日志
  log = (level: 'log' | 'info' | 'warn', message: any, queryRunner?: QueryRunner): any => {
    // log
  }
}

// 导出SqlLogger实例
export default SlqLogger.getInstance()
